package web.controller;

import Commons.enums.Gender;
import app.entity.AdminEntity;
import app.entity.Point;
import app.service.adminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller

public class IndexController {
    @Autowired
    private adminService adminService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/toIndex")
    public String toIndex(@ModelAttribute AdminEntity adminEntity){
        return "index";
    }
    @GetMapping("/user/id/{id}/age/{age}")
    public String pathVariable(@PathVariable Integer id,@PathVariable Integer age){
        return String.format("id:%d,age:%d",id,age);
    }
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/stu/index")
    public ModelAndView student(){
        ModelAndView modelAndView=new ModelAndView("index");
        return modelAndView;
    }
    @GetMapping("/response")
    public ResponseEntity<AdminEntity> responseEntity(){
        System.out.println(1/0);
        AdminEntity adminEntitye=new AdminEntity();
        adminEntitye.setAdmin("admin");
        adminEntitye.setPassword("123");
        ResponseEntity<AdminEntity>     responseEntity=new ResponseEntity<AdminEntity>(adminEntitye, HttpStatus.OK);
        return responseEntity;
    }
    @GetMapping("/admin")
    @ResponseBody
    public AdminEntity admin(){
        AdminEntity adminEntity=new AdminEntity();
        adminEntity.setAdmin("123");
        adminEntity.setPassword("123");
        return adminEntity;
    }
    @GetMapping(path = "/pets/{petId}",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public AdminEntity getAD(@PathVariable String petid){
        AdminEntity adminEntity=new AdminEntity();
        adminEntity.setPassword(petid);
        return adminEntity;
    }

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @ResponseBody
    public String upload(HttpServletRequest request, MultipartFile file) throws IOException {
        String uploadPath =request.getServletContext().getRealPath("/upload");
        File uploadFilePath =new File(uploadPath);
        if(!uploadFilePath.exists()){
            uploadFilePath.mkdir();
        }
       uploadPath+=file.getName();
        file.transferTo(new File(uploadPath));
        return "index";
    }
    @GetMapping("/point")
    public String point(@RequestParam(name = "point") Point point){
        return "index";
    }
    @GetMapping("/Gender")
    public String gender(@RequestParam(name = "gender")Gender gender){
        return "index";
    }
    @GetMapping("/getAdmin")
    @ResponseBody
    public AdminEntity getAdmin(){

        AdminEntity adminEntity=restTemplate.postForObject("http://localhost:6060/admins",null,AdminEntity.class);
        return adminEntity;
    }

}
